from common.logger_helper import g_wlogger
from common.utils import get_lastyear_mouth
from k3_data_report.business.cgtj_db import Cgtj

from k3_data_report.business.chtj_db import Chtj
from k3_data_report.business.config import bussiness_cal_fields

__author__ = "Ypf"
_date_ = "2019/7/22-9:07"


def compare_data_kh(dic1, dic2):
    for key_1 in dic1.keys():
        if key_1 in dic2.keys():
            dic1[key_1].append(dic2[key_1][2])
        else:
            dic1[key_1].append(0)
    for key_2 in dic2.keys():
        if key_2 not in dic1.keys():
            dic2[key_2].append(dic2[key_2][2])
            dic2[key_2][2] = 0
            dic1[key_2] = dic2[key_2]
    return dic1
def compare_data_pp(dic1, dic2):
    for key_1 in dic1.keys():
        if key_1 in dic2.keys():
            dic1[key_1].append(dic2[key_1][2])
        else:
            dic1[key_1].append(0)
    for key_2 in dic2.keys():
        if key_2 not in dic1.keys():
            dic2[key_2].append(dic2[key_2][2])
            dic2[key_2][2] = 0
            dic1[key_2] = dic2[key_2]
    return dic1
def baifenbi_data_kh(dic):
    for key in dic:
        a = dic[key][2]
        b = dic[key][3]
        if b == 0:
            dic[key].append(0)
        else:
            c = (a - b) / b
            dic[key].append(c)
    return dic
def compare_data_jl(dic1, dic2):
    for key_1 in dic1.keys():
        if key_1 in dic2.keys():
            dic1[key_1].append(dic2[key_1][1])
        else:
            dic1[key_1].append(0)
    for key_2 in dic2.keys():
        if key_2 not in dic1.keys():
            dic2[key_2].append(dic2[key_2][1])
            dic2[key_2][1] = 0
            dic1[key_2] = dic2[key_2]
    return dic1


def baifenbi_data_jl(dic):
    for key in dic:
        a = dic[key][1]
        b = dic[key][2]
        if b == 0:
            dic[key].append(0)
        else:
            c = (a - b) / b
            dic[key].append(c)
    return dic

def chtj_kh(time1):
    time2 = get_lastyear_mouth(time1)
    db_sql=bussiness_cal_fields['k3_caozuo']
    check_sql = db_sql["check_sql"] % ('k3chtjkh', time1)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic)==0:
        # 按照客户分类
        sql1_kh = db_sql["xsckd_chtj_kh"]%(time1)
        sql2_kh = db_sql["xsckd_chtj_kh"] % (time2)
        sql3_kh=db_sql["insert_chtj_kh"]
        chdtj_dic1_kh = Chtj.get_dic_kh(sql1_kh)
        chdtj_dic2_kh = Chtj.get_dic_kh(sql2_kh)
        chdtj_dic_kh = compare_data_kh(chdtj_dic1_kh, chdtj_dic2_kh)
        chdtj_kh=baifenbi_data_kh(chdtj_dic_kh)
        Chtj.insert_kh(sql3_kh,chdtj_kh,time1)
    else:
        g_wlogger.winfo(f"chtj_kh-{time1}:target_full_df, all data is empty.")
    # 按照客户经理品牌分类
    check_sql = db_sql["check_sql"] % ('k3chtjjl', time1)
    print(check_sql)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic)==0:
        sql1_jl = db_sql["xsckd_chtj_jl"] % (time1)
        sql2_jl = db_sql["xsckd_chtj_jl"] % (time2)
        sql3_jl= db_sql["insert_chtj_jl"]
        chdtj_dic1_jl = Chtj.get_dic_jl(sql1_jl)
        chdtj_dic2_jl = Chtj.get_dic_jl(sql2_jl)
        chdtj_dic_jl = compare_data_jl(chdtj_dic1_jl, chdtj_dic2_jl)
        chdtj_jl=baifenbi_data_jl(chdtj_dic_jl)
        Chtj.insert_jl(sql3_jl,chdtj_jl,time1)
    else:
        g_wlogger.winfo(f"chtj_jl-{time1}:target_full_df, all data is empty.")


def chtj_pp(time1):
    time2 = get_lastyear_mouth(time1)
    db_sql=bussiness_cal_fields['k3_caozuo']
    # 按照客户品牌分类
    check_sql = db_sql["check_sql"] % ('k3chtjcp', time1)
    check_dic = Cgtj.check_cf(check_sql)
    if len(check_dic)==0:
        sql1_pp = db_sql["xsckd_chtj_pp"]%(time1)
        sql2_pp = db_sql["xsckd_chtj_pp"] % (time2)
        sql3_pp=db_sql["insert_chtj_pp"]
        chdtj_dic1_pp = Chtj.get_dic_pp(sql1_pp)
        chdtj_dic2_pp = Chtj.get_dic_pp(sql2_pp)
        chdtj_dic_pp=compare_data_pp(chdtj_dic1_pp,chdtj_dic2_pp)
        chdtj_pp=baifenbi_data_kh(chdtj_dic_pp)
        Chtj.insert_pp(sql3_pp,chdtj_pp,time1)
    else:
        g_wlogger.winfo(f"chtj_pp-{time1}:target_full_df, all data is empty.")

def chtj_kh_main(month):
    try:
        chtj_kh(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"chtj_kh_main has error:{e} with:{month}")
        return False
def chtj_pp_main(month):
    try:
        chtj_pp(month)
        return True
    except Exception as e:
        g_wlogger.werror(f"chtj_pp_main has error:{e} with:{month}")
        return False
if __name__ == "__main__":
    time1='2019-09'
    time2='2018-09'
    chtj_pp(time1)




